# Maintainer: Dan McGee <dan@archlinux.org>
# Contributor: Eli Schwartz <eschwartz@archlinux.org>
# Contributor: Sebastien Binet <binet@lblbox>

pkgname=('python-pip' 'python2-pip')
pkgver=18.0
pkgrel=1
pkgdesc="The PyPA recommended tool for installing Python packages"
url="https://pip.pypa.io/"
arch=('any')
license=('MIT')
_deps=('setuptools' 'appdirs' 'cachecontrol' 'colorama' 'distlib' 'distro'
       'html5lib' 'lockfile' 'msgpack' 'packaging' 'progress' 'pyparsing' 'pytoml'
       'requests' 'retrying' 'six' 'webencodings')
makedepends=("${_deps[@]/#/python-}" "${_deps[@]/#/python2-}" 'python2-ipaddress' 'python-sphinx')
source=("pip-${pkgver}.tar.gz::https://github.com/pypa/pip/archive/${pkgver}.tar.gz")
sha512sums=('c8cedd6def77e9f96b79a1493d30ae8d95df7d06547980de0d105db0c8a5eff7710a1e55438834e97392b6a78702a8b3169cca8127572981129c30534646f975')

shopt -s extglob
prepare() {
  cd "$srcdir/pip-$pkgver"

  rm -rf src/pip/_vendor/!(__init__.py)
  sed -i 's/DEBUNDLED = False/DEBUNDLED = True/' src/pip/_vendor/__init__.py
}

build() {
  cd "$srcdir/pip-$pkgver"

  python setup.py build
  python2 setup.py build

  cd docs/
  PYTHONPATH="$srcdir/pip-$pkgver/src/" make man
  mkdir -p build/man-pip2
  cd build/man
  for manfile in *; do
    sed 's/pip/pip2/g;s/PIP/PIP2/g' $manfile > ../man-pip2/${manfile/pip/pip2}
  done
}

package_python-pip() {
  depends=("${_deps[@]/#/python-}")

  cd "$srcdir/pip-$pkgver"
  python setup.py install --prefix=/usr --root="$pkgdir"

  install -D -m644 LICENSE.txt \
	  "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  install -Dm644 -t "$pkgdir"/usr/share/man/man1 docs/build/man/*

  PYTHONPATH="$pkgdir"/usr/lib/python3.6/site-packages "$pkgdir"/usr/bin/pip completion --bash \
    | install -Dm644 /dev/stdin "$pkgdir"/usr/share/bash-completion/completions/pip
}

package_python2-pip() {
  depends=("${_deps[@]/#/python2-}" 'python2-ipaddress')
  conflicts=('python-pyinstall')
  replaces=('python-pyinstall')

  cd "$srcdir/pip-$pkgver"
  python2 setup.py install --prefix=/usr --root="$pkgdir"

  mv "$pkgdir/usr/bin/pip" "$pkgdir/usr/bin/pip2"
  sed -i "s|#!/usr/bin/env python$|#!/usr/bin/env python2|" \
    ${pkgdir}/usr/lib/python2.7/site-packages/pip/__init__.py
  python2 -m compileall ${pkgdir}/usr/lib/python2.7/site-packages/pip/__init__.py

  install -D -m644 LICENSE.txt \
	  "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  install -Dm644 -t "$pkgdir"/usr/share/man/man1 docs/build/man-pip2/*

  PYTHONPATH="$pkgdir"/usr/lib/python2.7/site-packages "$pkgdir"/usr/bin/pip2 completion --bash \
    | install -Dm644 /dev/stdin "$pkgdir"/usr/share/bash-completion/completions/pip2
}
